package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.NewCase;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.NewCaseMapper;
import cn.wolfcode.crm.query.impl.NewCaseQuery;
import cn.wolfcode.crm.service.INewCaseService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("newCase")
public class NewCaseController {
    @Autowired
    INewCaseService newCaseService;
    @Autowired
    NewCaseMapper newCaseMapper;
    @Autowired
    CustomerMapper customerMapper;

    @RequestMapping("view")
    @RequiresPermissions(value = {"newCase:view", "新增报表页面"}, logical = Logical.OR)
    public String view() {
        return "newCase";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public List<NewCase> getAll() {
        return newCaseService.getAll();
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"newCase:save", "保存新增报表"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(NewCase newCase,NewCaseQuery qo) {
        try {
            newCaseService.saveOrUpdate(newCase);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "列表更新失败");
        }
        return new JSONResult(true, "恭喜您执行成功");
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(NewCaseQuery qo) {
        return newCaseService.query(qo);
    }

    @RequestMapping("changeState")
    @RequiresPermissions(value = {"employee:change", "更改员工状态"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult changeState(Long id) {
        try {
            newCaseService.changeState(id);
        } catch (Exception e) {
            Customer customer = customerMapper.selectByName(newCaseMapper.selectByPrimaryKey(id).getName());
            e.printStackTrace();
            //防止回滚又实现ajax报告正常只能在这里用mapper的方法设置状态
            String s = "表单不完整,清完善表单";
            if (e.getMessage().equals(s)) {
                newCaseMapper.changeState(id, 4);
            }
            String s1 = "没有该用户,请检查保单信息是否与用户信息匹配";
            if (e.getMessage().equals(s1)) {
                newCaseMapper.changeState(id, 0);
            }
            String s2 = "没有该保单,请检查保单号";
            if (e.getMessage().equals(s2)) {
                customer.setState(false);
                customerMapper.updateByPrimaryKey(customer);
                newCaseMapper.changeState(id, 0);
            }
            String s3 = "没有该保单,车牌不匹配";
            if (e.getMessage().equals(s3)) {
                customer.setState(false);
                customerMapper.updateByPrimaryKey(customer);
                newCaseMapper.changeState(id, 0);
            }
            String s4 = "保单失效";
            if (e.getMessage().equals(s4)) {
                customer.setState(false);
                customerMapper.updateByPrimaryKey(customer);
                newCaseMapper.changeState(id, -1);
            }
            return new JSONResult(false, e.getMessage());
        }
        return new JSONResult(true, "状态更新成功");
    }
    @RequestMapping("validateName")
    @ResponseBody
    public boolean validateUsername(String name, Long id) {

        Customer customer = customerMapper.selectByName(name);
        return customer != null || customer.getId() == id;
    }
}
